package queue;

/**
 * @Description TODO
 * @Author zrj
 * @Date 2020/8/1 15:16
 * @Version 1.0
 */
public class LoopQueue implements Queue {

    private int[] data;
    private int front, tail;
    private int size;

    public LoopQueue(int capacity) {
        data = new int[capacity + 1];
        front = 0;
        tail = 0;
        size = 0;
    }

    public LoopQueue() {
        this(10);
    }

    public int getCapacity() {
        return data.length - 1;
    }


    public void enqueue(int e) {

        if ((tail + 1) % data.length == front) {
            resize(getCapacity() * 2);
        }
        data[tail] = e;
        tail = (tail + 1) % data.length;
        size++;
    }

    private void resize(int newCapacity) {
        int[] newData = new int[newCapacity];
        for (int i = 0; i < size; i++) {
            newData[i] = data[(i + front) % data.length];
        }
        data = newData;
        front = 0;
        tail = size;
    }

    public int dequeue() {
        int ret = data[front];
        front++;
        size--;
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0) {
            resize(getCapacity() / 2);
        }
        return ret;
    }

    public int getFront() {
        return 0;
    }

    public int getSize() {
        return 0;
    }

    public boolean isEmpty() {
        return front == tail;
    }
}
